package com.auto.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.nio.Buffer;
public class BOIServer {
    public static void main(String[] args) throws Exception {
        //创建线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        ServerSocket serverSocket = new ServerSocket(6666);
        System.out.println("服务器启动了");
        while (true) {
            final Socket socket = serverSocket.accept();
            System.out.println("连接到了一个客户端");
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    method(socket);
                }
            });
        }
    }

    public static void method(Socket socket) {
        byte[] bytes = new byte[1024];
        try {
            System.out.println("线程id" + Thread.currentThread().getId() + "线程名称" + Thread.currentThread().getName());
            while (true) {
                System.out.println("线程id" + Thread.currentThread().getId() + "线程名称" + Thread.currentThread().getName());

                InputStream inputStream = socket.getInputStream();
                int read = inputStream.read(bytes);
                if (read != -1) {
                    System.out.println("消息" + new String(bytes, 0, read));
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
